#!/usr/bin/python2

import os, sys
import subprocess

if not os.path.exists(os.path.join('etude','plugins-devel')):
    os.mkdir(os.path.join('etude', 'plugins-devel'))

GENERATE_TEMPLATES=False
if '--gen_templates' in sys.argv:
    print("Template generation enabled")
    GENERATE_TEMPLATES=True
    sys.argv.remove('--gen_templates')


cdir = os.path.dirname(os.path.abspath(__file__))
if os.path.exists('plugins'):
    os.chdir("plugins")
    for ppack in ("defaults", "extras", "visual"):
        os.chdir(ppack)
        try:
            sub_args = [sys.executable, "setup.py", "develop", "--install-dir", 
                os.path.join(cdir, "etude","plugins-devel"), "-m"]
            if GENERATE_TEMPLATES:
                sub_args.append('--gen_templates')
            subprocess.call(sub_args)
        except subprocess.CalledProcessError:
            print("Error creating "+ppack)
            sys.exit(1)
        os.chdir('..')

LANGUAGES = ('zh_CN','hu')

os.chdir(cdir)

DOMAIN='etude'
PACKAGES=['etude', 'etude/client', 'etude/gui']

#generate templates
if GENERATE_TEMPLATES:
    os.system("pygettext.py -d %s -p %s %s" % 
            (DOMAIN, "po", 
                "/*.py ".join([a.replace('.', '/') for a in PACKAGES])+"/*.py" )
            )

# Create mo files:
if not os.path.exists("locale"):
        os.mkdir("locale/")
for lang in LANGUAGES:
        pofile = "po/" + lang + ".po"
        mofile = "locale/"+ lang + "/LC_MESSAGES/etude.mo"
        if not os.path.exists("locale/" + lang + "/LC_MESSAGES/"):
                os.mkdir("locale/" + lang + "/")
                os.mkdir("locale/" + lang + "/LC_MESSAGES/")
        print "generating", mofile
        os.system("msgfmt %s -o %s" % (pofile, mofile))

